package com.ruoyi.web.ydz.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.web.ydz.domain.ReplyLike;
import com.ruoyi.web.ydz.mapper.ReplyLikeMapper;
import com.ruoyi.web.ydz.service.IReplyLikeService;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * 帖子评论点赞Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-29
 */
@Service
public class ReplyLikeServiceImpl implements IReplyLikeService
{
    @Resource
    private ReplyLikeMapper replyLikeMapper;

    @Resource
    private RedisCache redisCache;

    @Resource
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 查询帖子评论点赞
     * 
     * @param id 帖子评论点赞主键
     * @return 帖子评论点赞
     */
    @Override
    public ReplyLike selectReplyLikeById(Long id)
    {
        return replyLikeMapper.selectReplyLikeById(id);
    }

    /**
     * 查询帖子评论点赞列表
     * 
     * @param replyLike 帖子评论点赞
     * @return 帖子评论点赞
     */
    @Override
    public List<ReplyLike> selectReplyLikeList(ReplyLike replyLike)
    {
        return replyLikeMapper.selectReplyLikeList(replyLike);
    }

    /**
     * 新增帖子评论点赞
     * 
     * @param replyLike 帖子评论点赞
     * @return 结果
     */
    @Override
    public AjaxResult insertReplyLike(ReplyLike replyLike)
    {
        AjaxResult ajaxResult = new AjaxResult();

        try {
            if (Objects.isNull(replyLike.getInvitationId())) {
                ajaxResult.put("code", 200);
                ajaxResult.put("msg", "题目id不能为空");
                return ajaxResult;
            } else if (Objects.isNull(replyLike.getStatus())) {
                ajaxResult.put("code", 200);
                ajaxResult.put("msg", "点赞状态不能为空");
                return ajaxResult;
            } else if (Objects.isNull(replyLike.getReplyId())) {
                ajaxResult.put("code", 200);
                ajaxResult.put("msg", "评论id不能为空");
                return ajaxResult;
            }

            SysUser user = SecurityUtils.getLoginUser().getUser();
            replyLike.setCreateTime(DateUtils.getNowDate());
            replyLike.setCreateBy(user.getUserName());
            replyLike.setCreateName(user.getNickName());
            replyLike.setUserId(user.getUserId());

            /*点赞数量*/
            String ReplyLikeLike = "Reply:ReplyLikes:" + replyLike.getInvitationId() + ":" + replyLike.getReplyId();
            Integer currentAge = redisCache.getCacheObject(ReplyLikeLike);
            if (Objects.isNull(currentAge) || Integer.valueOf(currentAge) <= 0) {
                if ("0".equals(replyLike.getStatus())) {
                    redisCache.setCacheObject(ReplyLikeLike, 1);
                } else if ("1".equals(replyLike.getStatus())) {
                    /*若是取消点赞*/
                    redisCache.setCacheObject(ReplyLikeLike, 0);
                }
            } else if (Integer.valueOf(currentAge) > 0) {
                if ("0".equals(replyLike.getStatus())) {
                    redisCache.setCacheObject(ReplyLikeLike, Integer.valueOf(currentAge) + 1);
                } else if ("1".equals(replyLike.getStatus())) {
                    /*若是取消点赞*/
                    redisCache.setCacheObject(ReplyLikeLike, Integer.valueOf(currentAge) - 1);
                }
            }

            /*记录题目被谁点过赞*/
            String ReplyLikeLikesUser = "Reply:ReplyLikesUser:" + replyLike.getInvitationId() + ":" + replyLike.getReplyId();
            SetOperations<String, String> listOps = redisTemplate.opsForSet();
            if ("0".equals(replyLike.getStatus())) {
                listOps.add(ReplyLikeLikesUser, String.valueOf(replyLike.getUserId())); // 在列表右侧添加元素
                replyLikeMapper.insertReplyLike(replyLike);
            } else if ("1".equals(replyLike.getStatus())) {
                // 若是取消点赞
                listOps.remove(ReplyLikeLikesUser, 0, String.valueOf(replyLike.getUserId())); // 0 表示删除所有匹配的值
                replyLikeMapper.updateReplyLikeByUserId(replyLike);
            }

            /*记录点赞操作*/
            ListOperations<String, String> recordlist = redisTemplate.opsForList();
            recordlist.rightPush("Reply:ReplyLikesRecord:" + replyLike.getInvitationId() + ":" + replyLike.getReplyId(), JSONObject.toJSONString(replyLike)); // 在列表右侧添加元素

            ajaxResult.put("code",200);
            ajaxResult.put("msg","0".equals(replyLike.getStatus()) ? "点赞操作成功" : "取消点赞成功");
        } catch (Exception e) {
            e.printStackTrace();
            ajaxResult.put("code",500);
            ajaxResult.put("msg","操作失败,请联系管理员");
            ajaxResult.put("error",e.getMessage());
        }
        //return invitationLikeMapper.insertInvitationLike(invitationLike);
        return ajaxResult;
    }

    /**
     * 修改帖子评论点赞
     * 
     * @param replyLike 帖子评论点赞
     * @return 结果
     */
    @Override
    public int updateReplyLike(ReplyLike replyLike)
    {
        replyLike.setUpdateTime(DateUtils.getNowDate());
        return replyLikeMapper.updateReplyLike(replyLike);
    }

    /**
     * 批量删除帖子评论点赞
     * 
     * @param ids 需要删除的帖子评论点赞主键
     * @return 结果
     */
    @Override
    public int deleteReplyLikeByIds(Long[] ids)
    {
        return replyLikeMapper.deleteReplyLikeByIds(ids);
    }

    /**
     * 删除帖子评论点赞信息
     * 
     * @param id 帖子评论点赞主键
     * @return 结果
     */
    @Override
    public int deleteReplyLikeById(Long id)
    {
        return replyLikeMapper.deleteReplyLikeById(id);
    }

    /*
    * 根据评论id获取点赞总数
    * */
    @Override
    public HashMap<String,Object> getReplyLikes(Long invitationId,Long replyId){
        HashMap<String,Object> hashMap = new HashMap<>();

        Object cacheObject = redisCache.getCacheObject("Reply:ReplyLikes:" + invitationId + ":" + replyId);
        if (!Objects.isNull(cacheObject)){
            hashMap.put("likesCount",cacheObject);
        }
        return hashMap;
    }
}
